import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class WebThread extends Thread
{
	MultiThreadEnv my_delegate;
	String my_url = new String();
	
	private boolean still_retrieving = true;
	
	public void run()
	{
		
		while (still_retrieving)
		{
			String url = null;
			try
			{
				my_delegate.web_lock.lock();//CS
					url = my_delegate.getWebPages().poll();
				my_delegate.web_lock.unlock();//END CS
				
				if (url != null)
				{
					if (url.endsWith(".html") || url.endsWith(".html") || url.endsWith(".txt") || url.trim().endsWith("/"))
					{
						PageObject temp_page_object = new PageObject(url);
						Document document 			= Jsoup.connect(temp_page_object.getUrl()).timeout(6000).get();
						
						temp_page_object.setDocument(document);
						my_delegate.processDocument(temp_page_object);	
					}
					else
					{
						continue;
					}
				}
				
			}
			catch(Exception e)
			{
				if (url != null)
				{
					System.out.println("Could not navigate to URL: " + url  + e.getMessage());
				}
				continue;
			}
		}
	}
	public void setDelegate(MultiThreadEnv the_delegate)
	{
		my_delegate = the_delegate;
	}
	public void killRetrieve()
	{
		still_retrieving = false;
	}

}
